package com.tn.cron;

import java.util.List;

import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.tn.bo.Company;
import com.tn.dao.CompanyDAO;

/**
 * This class is responsible for finding all the accounts for which frequency is
 * Defined and it will generate separate task for each account submit it.
 * 
 * @author Amit Modi
 */
public class PaymentReportTaskGenerator {

	/** URL for payment report generator. */
	public static final String URL = "/generatePaymentReport";
	/** Parameter name for company unique id. */
	public static final String COMPANY_UNIQUE_ID = "COMPANY_UNIQUE_ID";
	/** Parameter name for company id. */
	public static final String COMPANY_ID = "COMPANY_ID";

	/**
	 * Method to generate all the tasks which are scheduled for today.
	 * 
	 * @return total number of tasks scheduled.
	 */
	public int generatePaymentReportTasks() {
		int count = 0;
		CompanyDAO dao = new CompanyDAO();
		Queue queue = QueueFactory.getQueue("payment-task-queue");
		List<Company> allCompanies = dao.findAllCompanies();
		// currently we are finding all the companies,
		// we need to come up with logic to identify which company has today as
		for (Company company : allCompanies) {
			TaskOptions task = TaskOptions.Builder.withDefaults();
			task.param(COMPANY_ID, company.getCompanyId());
			task.param(COMPANY_UNIQUE_ID, company.getCompanyUniqueIdentifier());
			// set the URL over here
			task.url(URL);
			queue.add(task);
			count++;
		}
		return count;
	}
}
